package com.wisdytech.ccdserver.common.utils;

import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 溯源帮助类
 *
 * @author svolt
 */
public class TraceCommonUtils {
    /**
     * 把需要处理的条件带@
     *
     * @param condParams
     * @param fields
     * @return
     */
    public static Map<String, Object> doConditionLogicByOther(Map<String, Object> condParams, String[] fields) {
        Set set = condParams.keySet();
        Iterator iterator = set.iterator();
        List<String> valList = null;
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            if(Arrays.asList(fields).contains(key)) {
                valList = new ArrayList<>();
                String value = condParams.get(key).toString();
                if (!value.contains("'")) {
                    valList = Arrays.asList(value.split(";"));
                    valList = valList.stream().distinct().collect(Collectors.toList());
                   // value = "'" + valList.stream().filter(string -> !StringUtils.isBlank(string)).collect(Collectors.joining("'@'")) + "'";
                    value = valList.stream().filter(string -> !StringUtils.isBlank(string)).collect(Collectors.joining("@"));
                    condParams.put(key, value);
                }
            }
        }
        return condParams;
    }

    /**
     * 把需要处理的条件带引号
     *
     * @param condParams
     * @param fields
     * @return
     */
    public static Map<String, Object> doConditionLogicBySemicolon(Map<String, Object> condParams,String[] fields) {
        Set set = condParams.keySet();
        Iterator iterator = set.iterator();
        List<String> valList = null;
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            if(Arrays.asList(fields).contains(key)) {
                valList = new ArrayList<>();
                String value = condParams.get(key).toString();
                if (!value.contains("'")) {
                    valList = Arrays.asList(value.split(","));
                    valList = valList.stream().distinct().collect(Collectors.toList());
                    value = "'" + valList.stream().filter(string -> !StringUtils.isBlank(string)).collect(Collectors.joining("','")) + "'";
                    condParams.put(key, value);
                }
            }
        }
        return condParams;
    }

}
